<?php

namespace backend\controllers;

use Yii;
use yii\web\BadRequestHttpException;

class PublicController extends \common\controllers\CommonController{
    
    public function __construct($id, $module, $config = array()) {
        parent::__construct($id, $module, $config);
        Yii::$app->name = $this->setting('site_name');
        $this->checkLimit();
    }

    private function checkLimit(){
        if(isset(Yii::$app->user->identity) && !empty(Yii::$app->user->identity)){
            $limitStr = Yii::$app->user->identity->limitStr;
            if($limitStr == ''){
                $limitStr = 'init';
            }
            $key = $limitStr.'_limit_str';
            $limit = $this->setting($key);
            if($limit != 'all'){
                $limits = explode(',', $limit);
                $action = $this->getAction();
                if(!in_array($action, $limits)){
                    echo '没有权限<a href="javascript:void(0);" onclick="window.history.go(-1);"> 返回</a>';
                    exit;
                }
            }
        }
    }

    

    /**
     * 获取当前Action
     * @return type
     */
    private function getAction(){
        $action = Yii::$app->request->url;
        if($action == '/'){
            return 'site/index';
        }
        $str = substr($action, 1, strlen($action));
        $strs = explode('?', $str);
        $s = explode('&', $strs[0]);
        return $s[0];
    }
}
